package top.codedance.iotp.server.net.handle.subhandle.controller;

import com.alibaba.fastjson.JSONObject;
import top.codedance.iotp.common.entity.UserEntity;
import top.codedance.iotp.common.enums.HttpStatus;
import top.codedance.iotp.server.ServerApplication;
import top.codedance.iotp.server.net.handle.WsChannelHandler;
import top.codedance.iotp.server.net.handle.subhandle.TextWsMsgHandler;
import top.codedance.iotp.server.net.handle.util.UserService;

import java.util.Arrays;
import java.util.Map;
import java.util.Set;

public abstract class AdapterWsController implements WsController {
    private JSONObject jsonObject;

    public void doHandler(WsChannelHandler wsChannelHandle, String clientId, JSONObject jsonObject) {
        this.jsonObject = jsonObject;
        doSubHandler(wsChannelHandle, clientId, jsonObject);
    }

    /**
     * 处理的controller需要实现
     * @param jsonObject
     * @return
     */
    public abstract void doSubHandler(WsChannelHandler wsChannelHandle, String clientId, JSONObject jsonObject);

    /**
     * 用户是否通过验证
     * @return
     */
    protected boolean userVerification() {
        String deviceId = jsonObject.getString("deviceId");
        String username = jsonObject.getString("username");
        String password = jsonObject.getString("password");
        UserEntity userEntity = new UserEntity();
        userEntity.setUsername(username);
        userEntity.setPassword(password);
        userEntity.setDeviceId(deviceId);
        if(ServerApplication.userVerifitionPrivateKey == null) {
            return UserService.isExits(userEntity);
        }else{
            return UserService.isExits(userEntity, ServerApplication.userVerifitionPrivateKey);
        }
    }

    /**
     * 管理员用户是否通过验证
     * @return
     */
    protected boolean adminVerification() {
        String username = jsonObject.getString("username");
        String password = jsonObject.getString("password");
        UserEntity userEntity = new UserEntity();
        userEntity.setUsername(username);
        userEntity.setPassword(password);
        if(ServerApplication.userVerifitionPrivateKey == null) {
            return UserService.isExitsOfAdmin(userEntity);
        }else{
            return UserService.isExitsOfAdmin(userEntity, ServerApplication.userVerifitionPrivateKey);
        }
    }

    /**
     * 清除发送包中关于服务端的验证参数
     * @param jo
     * @param outputJo
     * @return
     */
    protected JSONObject cleanServerParamsAndJoin(JSONObject jo, JSONObject outputJo){
        Set<Map.Entry<String, Object>> entries = jo.entrySet();
        for (Map.Entry<String, Object> entity : entries){
            if(Arrays.asList(new String[]{"username", "password", "agreement"}).contains(entity.getKey())){
                continue;
            }
            outputJo.put(entity.getKey(), entity.getValue());
        }
        return outputJo;
    }

    protected static String response(int statusCode, Object msg, Object data) {
        return TextWsMsgHandler.response(statusCode, msg, data);
    }

    protected static String okResponse(Object data) {
        return TextWsMsgHandler.okResponse(data);
    }

    protected static String errResponse(int statusCode, Object msg) {
        return TextWsMsgHandler.errResponse(statusCode, msg);
    }

    protected static String response500(String msg) {
        return TextWsMsgHandler.response500(msg);
    }

    protected static String response403(String msg){
        return errResponse(HttpStatus.FORBIDDEN.getCode(), msg);
    }
}
